/*******************************************************************
 *   > File Name: led.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: Thu 31 Oct 2019 10:19:24 PM CST
 ******************************************************************/
#define GPJ0CON		0xE0200240
#define GPJ0DAT		0xE0200244

#define rGPJ0CON	*((volatile unsigned int *)GPJ0CON)
#define rGPJ0DAT	*((volatile unsigned int *)GPJ0DAT)

#define GPD0CON     0xE02000A0
#define GPD0DAT     0xE02000A4

#define rGPD0CON	*((volatile unsigned int *)GPD0CON)
#define rGPD0DAT	*((volatile unsigned int *)GPD0DAT)

void delay(void);

// 该函数要实现led闪烁效果
void led_blink(void)
{
	// led初始化，也就是把GPJ0CON中设置为输出模式
	//volatile unsigned int *p = (unsigned int *)GPJ0CON;
	//volatile unsigned int *p1 = (unsigned int *)GPJ0DAT;
	rGPJ0CON = 0x11111111;
	rGPD0CON = 0x00000010;

	while (1) {
		// led亮
		rGPJ0DAT = ((0<<3) | (0<<4) | (0<<5));
		rGPD0DAT = (0<<1);
		// 延时
		delay();
		// led灭
		rGPJ0DAT = ((1<<3) | (1<<4) | (1<<5));
		rGPD0DAT = (1<<1);
		// 延时
		delay();
	}
}


void delay(void)
{
	volatile unsigned int i = 900000;		// volatile 让编译器不要优化，这样才能真正的减
	while (i--);							// 才能消耗时间，实现delay
}

void led_init(void)
{
    rGPJ0CON = 0x00111000;
    rGPD0CON = 0x00000010;

	rGPJ0DAT = ((1<<3) | (1<<4) | (1<<5));
	rGPD0DAT = (1<<1);
}

/*以下代码存在BUG,待分析或者串口打印运行结果*/
/*固件每次上电运行结果都有差异，与预期不符*/
void led_1_on(void)
{
    rGPJ0DAT &= ~(0x1<<3);//置0，输出低电平
}
void led_1_off(void)
{
    rGPJ0DAT |= (0x1<<3);//置1，输出高电平
}

void led_2_on(void)
{
    rGPJ0DAT &= ~(0x1<<4);//置0，输出低电平
}
void led_2_off(void)
{
    rGPJ0DAT |= (0x1<<4);//置1，输出高电平
}

void led_3_on(void)
{
    rGPJ0DAT &= ~(0x1<<5);//置0，输出低电平
}
void led_3_off(void)
{
    rGPJ0DAT |= (0x1<<5);//置1，输出高电平
}

void led_4_on(void)
{
	rGPD0DAT = (0<<1);
}
void led_4_off(void)
{
	rGPD0DAT = (1<<1);
}

int main(int argc, char* argv[])
{
    led_init();

    while(1)
    {
        led_1_on();
        delay();
        led_1_off();
        delay();

        led_2_on();
        delay();
        led_2_off();
        delay();
               
        led_3_on();
        delay();    
        led_3_off();
        delay();
    
        led_4_on();
        delay();
        led_4_off();
        delay();
    }

    return 0;
}
